/*
 * StringMap.h
 *
 *  Created on: Aug 25, 2011
 *      Author: Gherman Alin
 */

#ifndef TEMPLATEMAP_H__DATAACCESS
#define TEMPLATEMAP_H__DATAACCESS

#include <map>
#include <vector>
#include "serialization.h"

namespace DataAccess {

template<typename T>
class TemplateMap {
	friend class boost::serialization::access;

	template<class Archive>
	void serialize(Archive & ar, const unsigned int /* file_version */) {
		ar & data;
	}
public:
	TemplateMap(const std::map<std::string, T> & _data) :
			data(_data) {
	}

	TemplateMap() {
	}

	virtual ~TemplateMap() {
	}

	bool contains(const std::string& key) {
		return data.find(key) != data.end();
	}

	bool get(const std::string& key, T& out) {
		typename std::map<std::string, T>::iterator it = data.find(key);
		if (it == data.end()) {
			return false;
		}
		out = it->second;
		return true;
	}

	bool set(const std::string& key, const T& value) {
		data[key] = value;
		return true;
	}

	bool remove(const std::string& key) {
		return data.erase(key) == 1;
	}

	std::vector<std::string> getKeys() {
		std::vector<std::string> keys;
		typename std::map<std::string, T>::const_iterator it;
		for (it = data.begin(); it != data.end(); it++) {
			keys.push_back(it->first);
		}
		return keys;
	}

private:
	std::map<std::string, T> data;
};

typedef TemplateMap<std::string> StringMap;
typedef TemplateMap<std::vector<char> > BytesMap;

}

#endif
